/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc;

import com.cxhd.dc.actor.ActorManager;
import com.cxhd.dc.controller.CenterPatientController;
import com.cxhd.dc.controller.ConfigController;
import com.cxhd.dc.controller.IndexController;
import com.cxhd.dc.controller.PatientController;
import com.cxhd.dc.model.DataConfigModel;
import com.cxhd.dc.task.StartTask;
import com.cxhd.dc.tool.SqlFile;
import com.cxhd.dc.websocket.ConnectServer;
import com.cxhd.dc.websocket.StartServer;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import java.nio.file.Files;
import java.sql.SQLException;
import javax.servlet.ServletContext;

/**
 *
 * @author Administrator
 */
public class Config extends JFinalConfig {

    public StartTask st;
    public ConnectServer con;

    private StartServer server;

    private ActorManager actorManager;

    @Override

    public void configConstant(Constants c) {
        PropKit.use("a_little_config.txt");				// 加载少量必要配置，随后可用PropKit.get(...)获取值
        c.setDevMode(PropKit.getBoolean("devMode", false));
        c.setViewType(ViewType.JSP);

    }

    @Override
    public void configRoute(Routes routes) {
        routes.add("/", IndexController.class);
        routes.add("/config", ConfigController.class);
        routes.add("/patient", PatientController.class);
        routes.add("/center", CenterPatientController.class);
    }

    @Override
    public void configPlugin(Plugins plgns) {

        DruidPlugin druidPlugin_baseinfo = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
        plgns.add(druidPlugin_baseinfo);
        // 配置ActiveRecord插件
        ActiveRecordPlugin arp_baseinfo = new ActiveRecordPlugin("dcmris_en", druidPlugin_baseinfo);
        arp_baseinfo.setDialect(new MysqlDialect());
        plgns.add(arp_baseinfo);
        //arp_baseinfo.addMapping("dataconfig", "pk", DataConfigModel.class);

        DruidPlugin druidPlugin_pacsio = new DruidPlugin(PropKit.get("jdbcUrl3"), PropKit.get("user"), PropKit.get("password").trim());
        plgns.add(druidPlugin_pacsio);
        // 配置ActiveRecord插件
        ActiveRecordPlugin arp_pacsio = new ActiveRecordPlugin("pacsio", druidPlugin_pacsio);
        arp_pacsio.setDialect(new MysqlDialect());
        plgns.add(arp_pacsio);
        // System.out.println(PropKit.get("serverType").equalsIgnoreCase("center") + "_____________");
        if (PropKit.get("serverType").equalsIgnoreCase("center")) {

            DruidPlugin altasCenter = new DruidPlugin(PropKit.get("jdbcUrl4"), PropKit.get("user"), PropKit.get("password").trim());
            plgns.add(altasCenter);
            // 配置ActiveRecord插件
            ActiveRecordPlugin arp_altasCenter = new ActiveRecordPlugin("atlasCenter", altasCenter);
            arp_altasCenter.setDialect(new MysqlDialect());
            plgns.add(arp_altasCenter);

        }

    }

    @Override
    public void configInterceptor(Interceptors i) {

    }

    @Override
    public void configHandler(Handlers hndlrs) {

    }

    @Override
    public void afterJFinalStart() {
        super.afterJFinalStart();

        if (Db.use("dcmris_en").query("SELECT table_name FROM information_schema.TABLES WHERE table_name ='dataconfig'").isEmpty()) {

            Db.use("dcmris_en").update(SqlFile.dataConfig);
        }
        if (Db.use("dcmris_en").query("SELECT table_name FROM information_schema.TABLES WHERE table_name ='logstudy'").isEmpty()) {

            Db.use("dcmris_en").update(SqlFile.logStudy);
        }
        DataConfig.getInstance().getConfigForDatabase().checkFolder().CreateBat();
        actorManager = ActorManager.getIntance();
        actorManager.initActor();

        System.out.println(DataConfig.getInstance().getRecFileMode()+"==dataConfig");
        //是否启动任务
        if (DataConfig.getInstance().isAutoTask()) {
            st = StartTask.getIntance();
            st.start();
        }
        //是否启动websocket 
        if (DataConfig.getInstance().getStartWeb().equalsIgnoreCase("1")) {
            con = ConnectServer.getIntance();
            con.reConnect();
 
            
        }
        server = StartServer.getInstance();

    }

    @Override
    public void beforeJFinalStop() {
        super.beforeJFinalStop(); //To change body of generated methods, choose Tools | Templates.

        if (st != null) {
            st.stop();
        }
        if (con != null) {

            con.close();
        }
        if (server != null) {
            try {
                server.colseServer();
                server.stopSendThread();
            } catch (InterruptedException ex) {

            }

        }
        actorManager.destory();
    }

}
